//
//  XLDownloadImageTool.m
//  XiangLove
//
//  Created by 李传坤 on 2018/4/9.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLDownloadImageTool.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"

@implementation XLDownloadImageTool

#pragma mark - UIImageView加载图片
+ (void)downloadImage:(NSString *)url place:(UIImage *)place imageView:(UIImageView *)imageView
{
    if ( url && ![url isEqual:[NSNull null]] ) {
        __weak typeof(UIImageView *) imgTemp = imageView;
        [imgTemp setContentScaleFactor:[[UIScreen mainScreen] scale]];
        imgTemp.contentMode =  UIViewContentModeScaleAspectFill;
        imgTemp.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        imgTemp.clipsToBounds  = YES;
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:place options:SDWebImageLowPriority | SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
         
         {
             //             NSLog(@"wodeerrer%@",error);
             if (cacheType != SDImageCacheTypeMemory) {
                 imgTemp.alpha = 0.1f;
                 [UIView animateWithDuration:0.5f animations:^{
                     imgTemp.alpha = 1.0f;
                 }];
             }
             imgTemp.contentMode = image?UIViewContentModeScaleAspectFill:UIViewContentModeScaleToFill;
         }];
    }
    
}

+ (void)downloadImage:(NSString *)url completed:(DownloadImgCompletedBlock)completed imageView:(UIImageView *)imageView
{
    if ( url && ![url isEqual:[NSNull null]] ) {
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
         {
             if (completed != nil)
                 completed(image);
         }];
    }
}
#pragma mark - UIButton加载图片
+(void)downloadImage:(NSString *)url place:(UIImage *)place button:(UIButton *)btn{
    if ( url && ![url isEqual:[NSNull null]] ) {
        __weak typeof(UIButton *) btnTemp = btn;
        [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:url] forState:UIControlStateNormal placeholderImage:place options:SDWebImageLowPriority | SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (cacheType != SDImageCacheTypeMemory) {
                btnTemp.alpha = 0.1f;
                [UIView animateWithDuration:0.5f animations:^{
                    btnTemp.alpha = 1.0f;
                }];
            }
        }];
    }
}
@end
